package zdream.pmfield.stage.effect;

import java.util.function.Function;

import zdream.pmfield.data.foe.MoveAdditionalEffects;
import zdream.pmfield.stage.director.HookStore;
import zdream.pmfield.stage.director.HookType;
import zdream.pmfield.stage.director.Hooks;
import zdream.pmfield.stage.object.FoeActor;
import zdream.pmfield.stage.message.TimelineOrderChangeMessage;

/**
 * 触发后, 被击中的 Foe 的行动条在 {@link zdream.pmfield.stage.Timeline} 中将被推迟 0.5 回合
 *
 * @author Zdream
 * @date 2023-11-17
 * @since 0.0.1
 */
public class DelayEffect extends AbstractEffect {
	public DelayEffect() {
		super(MoveAdditionalEffects.E_DELAY);
	}

	@Override
	public void init(Function<HookType, HookStore.HookHandlerBuilder> onHook) {
		onHook.apply(HookType.AFTER_EXECUTE_ATTACK)
				.handle(this::applyEffect)
				.buildAndSave();
	}

	private void applyEffect(Hooks.HookContext rawContext) {
		final Hooks.AttackHookContext hooks = rawContext.as();

		for (FoeActor foe : affectedFoes(hooks.attackContext)) {
			applyFoe(foe);
		}
	}

	private void applyFoe(FoeActor foe) {
		TimelineOrderChangeMessage message = new TimelineOrderChangeMessage(foe);
		float from = foe.speedRemains;
		foe.speedRemains += 0.5f;
		float to = foe.speedRemains;
		pushMessage(foe.stage, message.withSpeedRemainChanged(from, to));
	}
}
